<?php
/**
 * 页面打印
 * @param $msgs
 */
function var_debug($msgs)
{
    echo '<pre>' . var_export($msgs, 1) . '</pre>';
}

/**
 * 页面打印
 * @param $msgs
 */
function D_B($msgs)
{
    var_debug($msgs);
}

/**
 * 正则验证
 * @param $value
 * @param $rule
 * @return bool
 */
function validate($value, $rule)
{
    $validateRule = array(
        'require' => '/\S+/',
        'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
        'url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(:\d+)?(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
        'currency' => '/^\d+(\.\d+)?$/',
        'number' => '/^\d+$/',
        'zip' => '/^\d{6}$/',
        'integer' => '/^[-\+]?\d+$/',
        'double' => '/^[-\+]?\d+(\.\d+)?$/',
        'english' => '/^[A-Za-z]+$/',
        'mobile' => '/^1[0-9]{10}$/',
        'date' => '/^(\d{4})-(\d{2})-(\d{2})$/',
        'password' => '/^[A-Za-z0-9]{4,20}$/',
        'newpassword' => '/^[A-Za-z0-9]{6,20}$/',
        'numberenglish' => '/^[A-Za-z0-9]+$/',
        'ch_en' => '/^[\x{4e00}-\x{9fa5}A-Za-z]+$/u', //匹配中英文
        'card_no' => '/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/', //身份证正则
    );
    // 检查是否有内置的正则表达式
    if (isset($validateRule[strtolower($rule)])) {
        $rule = $validateRule[strtolower($rule)];
    }
    return preg_match($rule, $value) === 1;
}

/**
 * 获取当前毫秒数
 * @return string
 */
function getMicrotime()
{
    $millTime = microtime();
    $timeInfo = explode(' ', $millTime);
    $milisTime = sprintf('%d%03d', $timeInfo[1], $timeInfo[0] * 1000);
    return $milisTime;
}

/**
 * 获取当前毫秒级别时间
 */
function getMicroDate()
{
    list($nowMicroSeconds, $nowTime) = explode(" ", microtime());
    $nowMilliSeconds = substr($nowMicroSeconds, 2, 3);
    $nowData = date('YmdHis', $nowTime) . $nowMilliSeconds;
    return $nowData;
}

/**
 * guid
 * @return string
 */
function guid()
{
    if (function_exists('com_create_guid')) {
        return md5(com_create_guid());
    } else {
        mt_srand((double)microtime() * 10000);
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $hyphen = chr(45);
        $uuid = substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
        return md5($uuid);
    }
}

/**
 * 判断当前访问是不是手机端访问
 * @return bool
 */
function isMobile()
{
    $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
    $mobileBrowser = '0';
    if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
        $mobileBrowser++;
    }
    if ((isset($_SERVER['HTTP_ACCEPT'])) && (strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'application/vnd.wap.xhtml+xml') !== false)) {
        $mobileBrowser++;
    }
    if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
        $mobileBrowser++;
    }
    if (isset($_SERVER['HTTP_PROFILE'])) {
        $mobileBrowser++;
    }
    $mobileUa = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
    $mobileAgents = array(
        'w3c ', 'acs-', 'alav', 'alca', 'amoi', 'audi', 'avan', 'benq', 'bird', 'blac',
        'blaz', 'brew', 'cell', 'cldc', 'cmd-', 'dang', 'doco', 'eric', 'hipt', 'inno',
        'ipaq', 'java', 'jigs', 'kddi', 'keji', 'leno', 'lg-c', 'lg-d', 'lg-g', 'lge-',
        'maui', 'maxo', 'midp', 'mits', 'mmef', 'mobi', 'mot-', 'moto', 'mwbp', 'nec-',
        'newt', 'noki', 'oper', 'palm', 'pana', 'pant', 'phil', 'play', 'port', 'prox',
        'qwap', 'sage', 'sams', 'sany', 'sch-', 'sec-', 'send', 'seri', 'sgh-', 'shar',
        'sie-', 'siem', 'smal', 'smar', 'sony', 'sph-', 'symb', 't-mo', 'teli', 'tim-',
        'tosh', 'tsm-', 'upg1', 'upsi', 'vk-v', 'voda', 'wap-', 'wapa', 'wapi', 'wapp',
        'wapr', 'webc', 'winw', 'winw', 'xda', 'xda-'
    );
    if (in_array($mobileUa, $mobileAgents)) {
        $mobileBrowser++;
    }
    if (strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false) {
        $mobileBrowser++;
    }
    // Pre-final check to reset everything if the user is on Windows
    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false) {
        $mobileBrowser = 0;
    }
    // But WP7 is also Windows, with a slightly different characteristic
    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false) {
        $mobileBrowser++;
    }
    if ($mobileBrowser > 0) {
        return true;
    } else {
        return false;
    }
}


/**
 * 文本日志
 * @param $data
 * @param string $fileName
 * @param int $logType
 */
function logs($data, $fileName = '', $logType = 3)
{
    $path = dirname($fileName);
    is_dir($path) or mkdir($path, 0777, true);

    $content = '-==============' . date('Y-m-d H:i:s') . '==============-' . PHP_EOL
        . var_export($data, true) . PHP_EOL . PHP_EOL;
    error_log($content, $logType, $fileName);
}

/**
 * curl工具
 * @param $url
 * @param $param
 * @param int $isPost
 * @param int $timeout
 * @return mixed
 */
function curl($url, $param, $isPost = 1, $timeout = 60)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, $isPost);

    if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    }

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
    $contents = curl_exec($ch);
    curl_close($ch);
    return json_decode($contents, true);
}


/**
 * 保存图片到服务器
 * @param $data 图片base64格式
 * @param $fileName
 * @return bool
 */
function saveImg($data, $fileName)
{
    if (empty($data) || empty($fileName)) {
        return false;
    }
    $path = dirname($fileName);
    is_dir($path) or mkdir($path, 0777, true);

    if (strstr($data, ",")) {
        $data = explode(',', $data);
        $image = $data[1];
    } else {
        $image = $data;
    }

    $r = file_put_contents($fileName, base64_decode($image));
    return $r ? true : false;
}



